<?php

namespace frontendw\models;

/**
 * @author  shuang
 * @date    2016-11-3 14:46:22
 * @version V1.0
 * @desc    
 */
use common\models\UserTaskOperate;
use Yii;
use common\components\MTools;
use yii\helpers\ArrayHelper;
use yii\db\Query;
use yii\behaviors\TimestampBehavior;

class WB_UserTaskOperate extends UserTaskOperate {

    public static $complain = [
        0 => "投诉",
        1 => "投诉中",
        2 => "投诉成功",
        3 => "投诉失败",
        4 => "申诉中",
        5 => "申诉成功",
        6 => "申诉失败"
    ];
    public static $status = [
        0 => "未上传",
        1 => "已上传"
    ];

    /*
     * 设置表操作行为动作
     * return array
     */

    public function behaviors() {
        return[
            [
                'class' => TimestampBehavior::className()
            ]
        ];
    }

    public function rules() {
        return ArrayHelper::merge(parent::rules(), [
                    [['taskaccount'], "match", "pattern" => "/^((0\\d{2,3}(-){0,1}\\d{7,8})|(1[35784]\\d{9}))$/", "message" => "手机号码格式不正确"],
        ]);
    }

    public function getTaskID() {

        return $this->hasOne(WB_Task::className(), ['id' => 'taskid']);
    }

    /*
     * 用户开始任务，添加记录
     * @params $userid
     * @params $taskid
     */

    public static function addOperateRecord($userid, $taskid) {
        $model = new WB_UserTaskOperate();
        $taskmodel = \frontend\models\WB_Vtasklist::find()->where("id=" . $taskid)->one();
        $model->userid = $userid;
        $model->taskid = $taskid;
        $model->taskuserid = $taskmodel->userid;
        $model->price = $taskmodel->brush_price;
        if ($model->save()) {
            return $model->id;
        } else {
            return array("errors" => $model->getErrors(), "model" => $model);
        }
    }

    /*
     * 查询做过的任务
     * @params $status
     */

    public static function getOperateTaskList($status = 0) {
        $query = new Query();
        $query->from(WB_UserTaskOperate::tableName() . " as a");
        $query->leftJoin(WB_Task::tableName() . " as b", "a.taskid=b.id");
        $query->leftJoin(WB_TaskPrice::tableName() . " as c", "b.task_price_id=c.id");
        $query->select("a.id,a.taskid,count(a.taskid) as operatecount,a.status,a.is_hidden,a.is_complain,b.title,b.single_meet,c.brush_price");
        $query->addGroupBy("a.taskid");
        $query->where("a.userid=:userid and a.status=:status", [":userid" => Yii::$app->user->id, ":status" => $status]);
        $query->orderBy("a.updated_at desc");
        $countQuery = clone $query;
        $defaultPageSize = 20;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => $defaultPageSize]);
        $offset = (Yii::$app->request->get("page") - 1) * $defaultPageSize;
        $limit = $defaultPageSize;
        $res = $query->offset($offset)->limit($limit)->all();
        return ["pager" => $pager, "data" => $res];
    }

    /*
     * 查询被投诉的任务
     */

    public static function getComplainTaskList() {
        $query = new Query();
        $query->from(WB_UserTaskOperate::tableName() . " as a");
        $query->leftJoin(WB_Task::tableName() . " as b", "a.taskid=b.id");
        $query->leftJoin(WB_TaskPrice::tableName() . " as c", "b.task_price_id=c.id");
        $query->select("a.id,a.updated_at,a.taskid,a.status,a.is_hidden,a.is_complain,b.title,a.complain_note,c.brush_price");
        $query->where("a.userid=:userid and is_complain in (2,4,5,6)", [":userid" => Yii::$app->user->id]);
        $countQuery = clone $query;
        $defaultPageSize = 20;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => $defaultPageSize]);
        $offset = (Yii::$app->request->post("page") - 1) * $defaultPageSize;
        $limit = $defaultPageSize;
        $res = $query->offset($offset)->limit($limit)->all();
        return ["pager" => $pager, "data" => $res];
    }

    /*
     * 查询隐藏的任务
     */

    public static function getHiddenTask($userid) {
        $result = WB_UserTaskOperate::find()->where("userid=" . $userid . " and is_hidden = 1")->select("taskid")->asArray()->all();
        return ArrayHelper::map($result, "taskid", "taskid");
    }

    /*
     * 微信端，查询用户可提交的任务
     * @params $openid
     * return object
     */

    public static function getWXCansubmitOneOfTask($openid, $status = 0) {
        $user = \common\models\User::find()->where(['wxopenid' => $openid])->one();
        if ($user) {
            $query = new Query();
            $query->from(WB_UserTaskOperate::tableName() . " as a");
            $query->leftJoin(WB_Task::tableName() . " as b", "a.taskid=b.id");
            $query->leftJoin(WB_TaskPrice::tableName() . " as c", "b.task_price_id=c.id");
            $query->select("a.id,a.taskid,count(a.taskid) as operatecount,a.status,a.is_hidden,a.is_complain,b.title,b.single_meet,c.brush_price,b.sample_figure_count,b.sample_figure");
            $query->addGroupBy("a.taskid");
            $query->where("a.userid=:userid and a.status=:status and b.is_offer_mobile=:default", [":userid" => $user->id, ":status" => $status, ":default" => 0]);
            $query->orderBy("a.updated_at desc");
            return $query->one();
        }
    }

}
